﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace HandWriter.Controls
{
	internal class ToolStripColorChooser : ToolStripDropDownButton
	{
		private ColorDialog _colorDialog;

		public Color CustomColor { get; set; }

		public Color SelectedColor { get; set; }

		public event EventHandler OnColorChanged;

		protected override void OnDropDownShow(EventArgs e)
		{
			DropDownItems.Clear();
			var cols =
				new [] {Color.Red, Color.Orange, Color.Yellow, Color.Green, Color.LightBlue, Color.Blue, Color.Violet};
			foreach (Color col in cols)
			{
				ToolStripItem n = new ToolStripColorItem(col, OnColorChangedInner);
				DropDownItems.Add(n);
			}

			ToolStripItem n2 = new ToolStripColorItem(CustomColor, OnColorChangedInner);
			n2.Text = "Custom " + CustomColor;
			DropDownItems.Add(n2);

			DropDownItems.Add(new ToolStripSeparator());

			ToolStripItem n3 = new ToolStripColorItem(Color.Empty, OnCustomClick);
			n3.Text = "Pick Custom Color ...";
			DropDownItems.Add(n3);
			base.OnDropDownShow(e);
		}

		private void OnColorChangedInner(object sender, EventArgs e)
		{
			var args = (ColorCheckedEventArgs) e;
			SelectedColor = args.Color;
			if (OnColorChanged != null)
			{
				OnColorChanged(sender, args);
			}
		}

		private void OnCustomClick(object sender, EventArgs e)
		{
			if (_colorDialog == null) _colorDialog = new ColorDialog();
			_colorDialog.Color = CustomColor;
			if (_colorDialog.ShowDialog() == DialogResult.OK)
			{
				CustomColor = _colorDialog.Color;
				OnColorChangedInner(sender, new ColorCheckedEventArgs(CustomColor));
			}
		}
	}
}